﻿using System.Linq;
using System.Web.Mvc;
using SklepISI.Core.Interfaces;
using SklepISI.Core.Models;
using SklepISI.Web.Models.Admin;

namespace SklepISI.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        private IProductRepository _productRepository;
        private IOrderService _orderService;
        private IUserService _userService;

        public AdminController(IProductRepository productRepository, IOrderService orderService, IUserService userService)
        {
            _productRepository = productRepository;
            _orderService = orderService;
            _userService = userService;
        }

        public ActionResult Index()
        {
            return View();
        }

        #region cateogires
        public ActionResult ListCategories()
        {
            var categories = _productRepository.GetCategories();

            return View(categories);
        }

        public ActionResult EditCategory(int id)
        {
            var category = _productRepository.GetCategories().Where(x => x.Id == id).FirstOrDefault();

            return View(category);
        }

        [HttpPost]
        public ActionResult EditCategory(CategoryModel model)
        {
            if (ModelState.IsValid)
            {
                _productRepository.UpdateCategory(model);

                return RedirectToAction("ListCategories");
            }

            return View(model);
        }

        public ActionResult AddCategory()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCategory(CategoryModel model)
        {

            if (ModelState.IsValid)
            {
                _productRepository.AddCategory(model);

                return RedirectToAction("ListCategories");
            }

            return View(model);
        }
        #endregion

        #region products
        public ActionResult ListProducts()
        {
            var products = _productRepository.GetProducts();

            return View(products);
        }

        public ActionResult EditProduct(int id)
        {
            var model = new ProductViewModel();
            var categories = _productRepository.GetCategories();

            model.Product = _productRepository.GetProducts().Where(x => x.ProductId == id).FirstOrDefault();
            model.Categories = new SelectList(categories, "Id", "Name", model.Product.CategoryId);

            return View(model);
        }

        [HttpPost]
        public ActionResult EditProduct(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                _productRepository.UpdateProduct(model.Product);
                return RedirectToAction("ListProducts");
            }

            model.Categories = new SelectList(_productRepository.GetCategories(), "Id", "Name", model.Product.CategoryId);

            return View(model);
        }

        public ActionResult AddProduct()
        {
            var model = new ProductViewModel();
            var cat = _productRepository.GetCategories();

            model.Categories = new SelectList(cat, "Id", "Name");

            return View(model);
        }

        [HttpPost]
        public ActionResult AddProduct(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                _productRepository.AddProduct(model.Product);

                return RedirectToAction("ListProducts");
            }

            model.Categories = new SelectList(_productRepository.GetCategories(), "Id", "Name", model.Product.ProductId);

            return View(model);
        }

        public ActionResult RemoveProduct(int id)
        {
            var mod = new ProductViewModel();
            var categor = _productRepository.GetCategories();

            mod.Product = _productRepository.GetProduct(id);
            //
            return View(mod);
        }


        [HttpPost]
        public ActionResult RemoveProduct(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                _productRepository.RemoveProduct(model.Product.ProductId);
                return RedirectToAction("ListProducts");
            }
            return View(model);
            //
        }
        #endregion

        #region orders
        public ActionResult ListOrders()
        {
            var orders = _orderService.GetOrders();

            return View(orders);
        }

        public ActionResult OrderDetails(int id)
        {
            var order = _orderService.GetOrder(id);

            if (order != null)
                order.User = _userService.GetUserById(order.UserID);

            var viewModel = new OrderDetailViewModel
                                {
                                    OrderStatuses = _orderService.GetOrderStatuses(),
                                    Order = order
                                };

            return View(viewModel);
        }

        public JsonResult ChangeOrderStatus(int orderId, int statusId)
        {
            var order = _orderService.GetOrder(orderId);
            var status = _orderService.GetStatus(statusId);

            if (order != null && status != null)
            {
                order.Status = status;
                _orderService.UpdateOrder(order);

                return Json("OK");
            }

            return Json("invalid OrderId or StatusId");
        }

        #endregion
    }
}
